package com.java.userservice.controller;

import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @author cf
 * @date 2021/10/25 16:09
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 测试
     *
     * @param authentication 头部
     * @param request        请求
     * @return 返回
     */
    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request) {
//        return authentication;
//        return authentication.getPrincipal();//这样会直接返回所有的包含内容
        String header = request.getHeader("Authorization");
        String token = StrUtil.subAfter(header, "Bearer ", false);
        Claims body = Jwts.parser()
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
        log.info(String.valueOf(body));
        return body;
    }

}